package com.gykfbxxh.imovie.adapter.pageAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.custom.view.MyViewPager;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;

import java.util.ArrayList;
import java.util.List;

import static com.gykfbxxh.imovie.base.BaseActivity.PARAMETER1;

/**
 * 新闻轮播图适配器
 *
 * @author xuxinghai
 */
public class NewsPicPagerAdapter extends PagerAdapter implements OnPageChangeListener, OnClickListener {
    private ArrayList<View> list;
    private Context context;
    private ViewGroup smallList;
    private List<FilmEntity> pics;
    private MyViewPager viewPager;
    private int pagePosition;

    public ArrayList<View> getList() {
        return list;
    }

    public void setList(ArrayList<View> list) {
        this.list = list;
    }

    public void removeItem(View view, int position) {
        list.remove(view);
        pics.remove(position);
        smallList.removeViewAt(0);
        notifyDataSetChanged();
    }

    public NewsPicPagerAdapter(ArrayList<View> list, ViewGroup smalllist, List<FilmEntity> pics, Context context, MyViewPager viewPager) {
        this.list = list;
        this.viewPager = viewPager;
        this.context = context;
        this.smallList = smalllist;
        this.pics = pics;
    }

    @Override
    public int getCount() {
        return list != null && !list.isEmpty() ? list.size() : 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        container.addView(list.get(position));
        if (list.get(position) instanceof ImageView) {
            list.get(position).setOnClickListener(this);
        }
        return list.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        if (arg0 == 0) {
            if (pagePosition < 1) {
                viewPager.setCurrentItem(list.size() - 2, false);
            } else if (pagePosition > list.size() - 2) {
                viewPager.setCurrentItem(1, false);
            }
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int positon) {
        pagePosition = positon;
        if (positon < 1) {
            positon = list.size() - 3;
        } else if (positon > list.size() - 2) {
            positon = 0;
        } else {
            positon--;
        }
        for (int i = 0; i < smallList.getChildCount(); i++) {
            if (i == positon) {
                ((ImageView) smallList.getChildAt(i)).setImageResource(R.drawable.drop_1);
                ((ImageView) smallList.getChildAt(i)).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            } else {
                ((ImageView) smallList.getChildAt(i)).setImageResource(R.drawable.drop_2);
                ((ImageView) smallList.getChildAt(i)).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }
        }
    }

    @Override
    public void onClick(View view) {
        FilmEntity pic = (FilmEntity) view.getTag(R.id.tag_first);
        Intent intent = new Intent(context, FilmDetailActivity.class);
        intent.putExtra(PARAMETER1,pic.getFId());
        ((Activity)context).startActivity(intent);
    }

}
